<?php
namespace Home\Controller;

use Common\Service\Email;
use Think\Controller;
use Think\Log;

//查找未完成未逾期的数据推送
class CrontabController extends Controller
{
    //高级经办人id
    private $highIds = [5, 6, 7, 8, 19, 20];

    /**
     * 每天9点  3-10天的推送消息
     * @return [type] [description]
     */
    public function crontab1()
    {
        set_time_limit(0);
        $model           = M('task');
        $userModel       = M('user');
        $emailLog        = M('send_email_log');
        $where['status'] = ['eq', 2];
        $data            = $model->where($where)->select();
        //高级经办者
        $highData = $userModel->where('role=1')->select();

        foreach ($data as $key => $value) {
            $today = time();
            $end   = $value['end_time'];
            $dif   = ceil(($end - $today) / 86400);
            //去掉逾期的数据,没有抄送者跟进者数据
            if ($dif >= 3 && $dif <= 10 && ($value['follow_user_id'] || $value['cc_user_id'])) {
                //有高级经办人就是，下单人就是高级经办人，没有就是创建任务本人
                $id       = is_null($value['audit_user_id']) ? $value['input_user_id'] : $value['audit_user_id'];
                $userInfo = $userModel->where('id=' . $id)->find();
                //跟进者
                $follows         = explode(',', $value['follow_user_id']);
                $follows         = array_filter($follows);
                $where2['id']    = ['in', $follows];
                $followsUserData = $userModel->where($where2)->select();
                $followName      = '';
                foreach ($followsUserData as $key1 => $value1) {
                    $followName .= $value1['nickname'] . ',';
                }
                $followName = substr($followName, 0, strlen($followName) - 1);
                //推送人的id
                $tmp[$key]['ids']         = $value['follow_user_id'] . ',' . $value['cc_user_id'];
                $tmp[$key]['task_id']     = $value['id'];
                $tmp[$key]['days']        = $dif; //距离多少天
                $tmp[$key]['no']          = $value['sn']; //任务编号
                $tmp[$key]['name']        = $value['name']; //任务名称
                $tmp[$key]['create_time'] = date('Y-m-d H:i:s', $value['add_time']); //任务创建时间
                $tmp[$key]['end_time']    = date('Y-m-d H:i:s', $value['end_time']); //任务截止时间
                $tmp[$key]['uname']       = $userInfo['nickname']; //创建人
                $tmp[$key]['followName']  = $followName; //跟进人
                $tmp[$key]['remark']      = $value['remark']; //任务提醒
            }
        }

        $tmp = [];
        if ($tmp) {
            foreach ($tmp as $key => $value) {
                $idsArr       = explode(',', $value['ids']);
                $idsArr       = $this->filter_ids($idsArr);
                $where1['id'] = ['in', $idsArr];
                $userData     = $userModel->where($where1)->select();
                $content      = Email::sendEmailContent($value['task_id'], $value['no'], $value['name'], $value['create_time'], $value['end_time'], $value['uname'], $value['followName'], $value['remark']);

                //推送消息 高级经办者
                foreach ($highData as $key1 => $value1) {
                    $res = Email::sendEmail($value1['email'], $value['name'], $content);
                    sleep(10);
                    Log::write('每天早上九点发送邮件高级经办者日志记录：' . $res);
                }

                //推送消息 相关人员
                foreach ($userData as $key2 => $value2) {
                    $res1 = Email::sendEmail($value2['email'], $value['name'], $content);
                    sleep(10);
                    Log::write('每天早上九点发送邮件相关人员日志记录：' . $res1);
                }

                //记录发送邮件的详情
                $insertData['task_id']     = $value['task_id'];
                $insertData['ids']         = $value['ids'];
                $insertData['create_time'] = time();
                $insertData['dis_days']    = $value['days'];
                $insertData['no']          = $value['no'];
                $res2                      = $emailLog->add($insertData);
                Log::write('每天早上九点发送邮件数据库记录结果：' . $res2);
            }
        } else {
            Log::write('每天早上九点发送邮件数据为空');
        }
    }

    /**
     * 每天9 13 17 20点   小于3天的推送消息
     * @return [type] [description]
     */
    public function crontab2()
    {
        set_time_limit(0);
        $model           = M('task');
        $userModel       = M('user');
        $emailLog        = M('send_email_log');
        $where['status'] = ['eq', 2];
        $data            = $model->where($where)->select();
        //高级经办者
        $highData = $userModel->where('role=1')->select();
        $tmp      = [];
        foreach ($data as $key => $value) {
            $today = time();
            $end   = $value['end_time'];
            $dif   = ceil(($end - $today) / 86400);

            //去掉逾期的数据,没有抄送者跟进者数据
            if ($dif < 3 && ($value['follow_user_id'] || $value['cc_user_id'])) {
                //有高级经办人就是，下单人就是高级经办人，没有就是创建任务本人
                $id       = is_null($value['audit_user_id']) ? $value['input_user_id'] : $value['audit_user_id'];
                $userInfo = $userModel->where('id=' . $id)->find();
                //跟进者
                $follows         = explode(',', $value['follow_user_id']);
                $follows         = array_filter($follows);
                $where2['id']    = ['in', $follows];
                $followsUserData = $userModel->where($where2)->select();
                $followName      = '';
                foreach ($followsUserData as $key1 => $value1) {
                    $followName .= $value1['nickname'] . ',';
                }
                $followName = substr($followName, 0, strlen($followName) - 1);
                //推送人的id
                $tmp[$key]['ids']         = $value['follow_user_id'] . ',' . $value['cc_user_id'];
                $tmp[$key]['task_id']     = $value['id'];
                $tmp[$key]['days']        = $dif; //距离多少天
                $tmp[$key]['no']          = $value['sn']; //任务编号
                $tmp[$key]['name']        = $value['name']; //任务名称
                $tmp[$key]['create_time'] = date('Y-m-d H:i:s', $value['add_time']); //任务创建时间
                $tmp[$key]['end_time']    = date('Y-m-d H:i:s', $value['end_time']); //任务截止时间
                $tmp[$key]['uname']       = $userInfo['nickname']; //创建人
                $tmp[$key]['followName']  = $followName; //跟进人
                $tmp[$key]['remark']      = $value['remark']; //任务提醒
            }
        }

        if ($tmp) {
            foreach ($tmp as $key => $value) {
                $idsArr = explode(',', $value['ids']);
                $idsArr = $this->filter_ids($idsArr);
                //exit(json_encode($idsArr));die;
                $where1['id'] = ['in', $idsArr];
                $userData     = $userModel->where($where1)->select();
                $content      = Email::sendEmailContent($value['task_id'], $value['no'], $value['name'], $value['create_time'], $value['end_time'], $value['uname'], $value['followName'], $value['remark']);

                // $res1 = Email::sendEmail('1028906591@qq.com', $value['name'], $content);
                // var_dump($res1);die;

                //推送消息 高级经办者
                foreach ($highData as $key1 => $value1) {
                    $res = Email::sendEmail($value1['email'], $value['name'], $content);
                    sleep(10);
                    Log::write('每天9 13 17 20点发送邮件高级经办者日志记录：' . $res);
                }

                //推送消息 相关人员
                foreach ($userData as $key2 => $value2) {
                    $res1 = Email::sendEmail($value2['email'], $value['name'], $content);
                    sleep(10);
                    Log::write('每天9 13 17 20点发送邮件相关人员日志记录：' . $res1);
                }

                //记录发送邮件的详情
                $insertData['task_id']     = $value['task_id'];
                $insertData['ids']         = $value['ids'];
                $insertData['create_time'] = time();
                $insertData['dis_days']    = $value['days'];
                $insertData['no']          = $value['no'];
                $res2                      = $emailLog->add($insertData);
                Log::write('每天9 13 17 20点发送邮件数据库记录结果：' . $res2);
            }
        } else {
            Log::write('每天9 13 17 20点发送邮件数据为空');
        }
    }

    public function crontab3()
    {
        set_time_limit(0);
        $model           = M('task');
        $userModel       = M('user');
        $emailLog        = M('send_email_nine');
        $where['status'] = ['eq', 2];
        $data            = $model->where($where)->select();
        //高级经办者
        //$highData = $userModel->where('role=1')->select();
        $tmp = [];
        foreach ($data as $key => $value) {
            $today = time();
            $end   = $value['end_time'];
            $dif   = ceil(($end - $today) / 86400);
            if ($value['follow_user_id'] || $value['cc_user_id']) {
                //有高级经办人就是，下单人就是高级经办人，没有就是创建任务本人
                $id       = is_null($value['audit_user_id']) ? $value['input_user_id'] : $value['audit_user_id'];
                $userInfo = $userModel->where('id=' . $id)->find();
                //跟进者
                $follows = explode(',', $value['follow_user_id']);
                $follows = array_filter($follows);
                $where2['id']    = ['in', $follows];
                $followsUserData = $userModel->where($where2)->select();
                $followName      = '';
                foreach ($followsUserData as $key1 => $value1) {
                    $followName .= $value1['nickname'] . ',';
                }
                $followName = substr($followName, 0, strlen($followName) - 1);
                //推送人的id
                $tmp[$key]['ids']        = $value['follow_user_id'] . ',' . $value['cc_user_id'];
                $tmp[$key]['task_id']    = $value['id'];
                $tmp[$key]['dis_days']   = $dif; //距离多少天
                $tmp[$key]['no']         = $value['sn']; //任务编号
                $tmp[$key]['task_name']  = $value['name']; //任务名称
                $tmp[$key]['ctime']      = $value['add_time']; //任务创建时间
                $tmp[$key]['etime']      = $value['end_time']; //任务截止时间
                $tmp[$key]['uname']      = $userInfo['nickname']; //创建人
                $tmp[$key]['follow_name'] = $followName; //跟进人
                $tmp[$key]['remark']     = $value['remark']; //任务提醒
                $tmp[$key]['status']     = 0; //任务提醒
                $tmp[$key]['add_time']   = time(); //任务提醒
            }
        }

        $rs = $emailLog->addAll($tmp);
        var_dump($rs);die;
        exit(json_encode($tmp));

    }

    /**
     * 普通经办人去掉高级经办人id，避免重复发送邮件
     * @return [type] [description]
     */
    private function filter_ids($arr)
    {
        $ret = array_diff($arr, $this->highIds);
        return $ret;
    }
}
